package hu.mapro.gwtui.gxt.client.recordseditor.requestfactory.supplier;

import hu.mapro.gwtui.gxt.client.recordseditor.requestfactory.RequestContextHolderInterface;

import com.google.common.base.Supplier;
import com.google.web.bindery.requestfactory.shared.RequestContext;

public class RequestContextHolderAdapter<RC extends RequestContext> implements RequestContextHolderInterface<RC> {

	private Supplier<? extends RC> requestContextFactory;
	
	public RequestContextHolderAdapter(
			Supplier<? extends RC> requestContextFactory) {
		super();
		this.requestContextFactory = requestContextFactory;
	}

	private RC currentRequestContext;

	@Override
	public void newRequestContext() {
		currentRequestContext = requestContextFactory.get();
	}

	@Override
	public RC getCurrentRequestContext() {
		return currentRequestContext;
	}
	
	public static <RC extends RequestContext> RequestContextHolderAdapter<RC> create(Supplier<? extends RC> supplier) {
		return new RequestContextHolderAdapter<RC>(supplier);
	}

}
